package com.example.aidldclient;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.view.KeyEventDispatcher;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.aidlserver.*;
import com.example.aidlserver.aidl.Book;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_connection_remote_service).setOnClickListener(this);
        findViewById(R.id.btn_add_remote_data).setOnClickListener(this);
        findViewById(R.id.btn_get_remote_date).setOnClickListener(this);
    }
    InterBookManager interBookManager;
    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
             interBookManager = InterBookManager.Stub.asInterface(service);
            Toast.makeText(MainActivity.this,"远程成功",Toast.LENGTH_LONG);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Toast.makeText(MainActivity.this,"断开",Toast.LENGTH_LONG);
            interBookManager = null;
        }
    };
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id==R.id.btn_connection_remote_service){
            Intent remoteServerIntent = new Intent("com.example.aidlserver.RemoteServer");

            PackageManager packageManager = getPackageManager();
            List<ResolveInfo> resolveInfos = packageManager.queryIntentServices(remoteServerIntent, 0);
            if(resolveInfos==null||resolveInfos.size()!=1){
                Toast.makeText(MainActivity.this,"找不到服务",Toast.LENGTH_LONG);
                return;
            }
            ResolveInfo resolveInfo = resolveInfos.get(0);
            String packageName = resolveInfo.serviceInfo.packageName;
            String className = resolveInfo.serviceInfo.name;
            ComponentName componentName = new ComponentName(packageName,className);

            Intent intent  = new Intent(remoteServerIntent);
            intent.setComponent(componentName);


            bindService(intent,serviceConnection,BIND_AUTO_CREATE);
        }else if(id==R.id.btn_add_remote_data){
            Book book = new Book();
            book.name = "书名"+index;
            book.bookId = index;
            book.prise = index*index;
            try {
                interBookManager.addBook(book);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            index++;
        }else if(id==R.id.btn_get_remote_date){
            try {
                List<Book> books = interBookManager.getBooks();
                Toast.makeText(MainActivity.this,"通信成功"+books.size(),Toast.LENGTH_SHORT);
            } catch (RemoteException e) {
                Toast.makeText(MainActivity.this,"获取数据失败",Toast.LENGTH_LONG);
                e.printStackTrace();
            }
        }
    }
    int index = 1;
}
